#import "URLWeb.h"
#import "SDAutoLayout.h"
#import "UIHelper.h"
#import "AppDelegate.h"
@interface URLWeb ()<UIWebViewDelegate>
@property (nonatomic , weak) UIWebView *web;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@end
@implementation URLWeb
- (UIWebView *)web
{
    if (!_web) {
    UIWebView *web = [[UIWebView alloc]init];
        web.delegate = self;
    [self.view addSubview:web];
    if (@available(iOS 11,*)) {
            web.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    web.sd_layout
    .leftEqualToView(self.view)
    .topEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomSpaceToView(self.view, 49);
    web.scrollView.bounces = NO;
        _web = web;
    }
    return _web;
}
- (IBAction)exitApp:(UIButton *)sender {
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:@"温馨提示"
                                 message:@"是否退出"
                                 preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"确定"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                    [UIView animateWithDuration:0.25 animations:^{
                                        self.web.alpha = 0;
                                    } completion:^(BOOL finished) {
                                        exit(0);
                                    }];
                                }];
    UIAlertAction* noButton = [UIAlertAction
                               actionWithTitle:@"取消"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                               }];
    [alert addAction:yesButton];
    [alert addAction:noButton];
    [self presentViewController:alert animated:alert completion:nil];
}
- (IBAction)goForward:(UIButton *)sender {
    if ([self.web canGoForward]) {
        [self.web goForward];
    }
}
- (IBAction)goBack:(UIButton *)sender {
    if ([self.web canGoBack]) {
        [self.web goBack];
    }
}
- (IBAction)refreshPage:(UIButton *)sender {
    [self.web reload];
}
- (IBAction)mainPage:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:self.reLastURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.web loadRequest:request];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
     AppDelegate * delegate = ( AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 1;
}
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [self forwordww];
}
- (void)forwordww
{
    UIDeviceOrientation oriention = [UIDevice currentDevice].orientation;
    UIInterfaceOrientation interfaceOriention = (UIInterfaceOrientation)oriention;
    switch (interfaceOriention) {
        case UIInterfaceOrientationUnknown:
            NSLog(@"位置方向");
            break;
        case UIInterfaceOrientationPortrait:
            NSLog(@"第0个旋转方向---电池栏在上");
            if (self.bottomView.hidden) {
                self.bottomView.hidden = NO;
            }
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            NSLog(@"第3个旋转方向---电池栏在下");
            break;
        case UIInterfaceOrientationLandscapeLeft:
            NSLog(@"第2个旋转方向---电池栏在右");
            self.bottomView.hidden = YES;
            self.web.zq_height += self.bottomView.zq_height;
            break;
        case UIInterfaceOrientationLandscapeRight:
            NSLog(@"第1个旋转方向---电池栏在左");
            self.bottomView.hidden = YES;
            self.web.zq_height += self.bottomView.zq_height;
            break;
        default:
            break;
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    AppDelegate * delegate = ( AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 0;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:self.reLastURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.web loadRequest:request];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIHelper showLoadingInView:self.view];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
        [ UIHelper dismissLoadingInView:self.view];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (BOOL)prefersStatusBarHidden{
    return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
@end
